home *** CD-ROM | disk | FTP | other *** search
/ MacGames Sampler / PHT MacGames Bundle.iso / MacSource Folder / Samples from the CD / Editors / emacs / Emacs-1.14b1-sources / sources / unix-emulation-src / ndir.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-05-29  |  2.6 KB  |  121 lines  |  [TEXT/EMAC]

  1. /*
  2.  * Copyright (C) 1993, 1994 Marc Parmet.
  3.  * This file is part of the Macintosh port of GNU Emacs.
  4.  *
  5.  * GNU Emacs is distributed in the hope that it will be useful,
  6.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  7.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  8.  * GNU General Public License for more details.
  9.  */
  10.  
  11. #if defined(THINK_C)
  12. #include <MacHeaders>
  13. #else
  14. #include <Types.h>
  15. #include <Memory.h>
  16. #include <Quickdraw.h>
  17. #include <Windows.h>
  18. #include <Files.h>
  19. #include <Errors.h>
  20. #endif
  21.  
  22. #include "sys/dir.h"
  23.  
  24. DIR *
  25. opendir_internal(char *filename)
  26. {
  27.     short err;
  28.     FSSpec fs;
  29.     CInfoPBRec pb;
  30.     DIR *dirp = 0L;
  31.  
  32.     dirp = (DIR *)NewPtr(sizeof(DIR));
  33.     err = MemError();
  34.     if (err) goto error;
  35.  
  36.     err = unixfn2FSSpec_internal(filename,&fs,0);
  37.     if (err == nsDrvErr) {
  38.         // Want to look at root.  Need to prepare a list of all volumes.
  39.         dirp->dd_volumes = 1;
  40.         dirp->dd_index = 0;
  41.         return dirp;
  42.     }
  43.     else if (err)
  44.         goto error;
  45.     else {
  46.         pb.dirInfo.ioFDirIndex = 0;
  47.         pb.dirInfo.ioNamePtr = fs.name;
  48.         pb.dirInfo.ioVRefNum = fs.vRefNum;
  49.         pb.dirInfo.ioDrDirID = fs.parID;
  50.         err = PBGetCatInfo(&pb,0);
  51.         if (err) goto error;
  52.         if (!(pb.dirInfo.ioFlAttrib & 0x10)) goto error; // Not a directory
  53.     
  54.         dirp->dd_volumes = 0;
  55.         dirp->dd_vRefNum = fs.vRefNum;
  56.         dirp->dd_drDirID = pb.dirInfo.ioDrDirID;
  57.         dirp->dd_index = -1;
  58.     }
  59.  
  60.     return dirp;
  61.  
  62.     error:
  63.     set_errno(err);
  64.     if (dirp) DisposPtr((Ptr)dirp);
  65.     return 0L;
  66. }
  67.  
  68. int
  69. closedir_internal(DIR *dirp)
  70. {
  71.     DisposPtr((Ptr)dirp);
  72.     return 0;
  73. }
  74.  
  75. struct direct *
  76. readdir_internal(DIR *dirp)
  77. {
  78.     short err;
  79.     CInfoPBRec pb1;
  80.     HParamBlockRec pb2;
  81.     static struct direct result;
  82.     
  83.     if (dirp->dd_volumes) {
  84.         if (dirp->dd_index == 0) {
  85.             strcpy(result.d_name,".");
  86.             ++dirp->dd_index;
  87.         }
  88.         else {
  89.             pb2.volumeParam.ioVolIndex = dirp->dd_index++;
  90.             pb2.volumeParam.ioNamePtr = (unsigned char *)result.d_name;
  91.             err = PBHGetVInfo(&pb2,0);
  92.             if (err) { set_errno(err); return 0L; };
  93.             PtoCstr((unsigned char *)result.d_name);
  94.         }
  95.     }
  96.     else {
  97.         if (dirp->dd_index == -1) {
  98.             strcpy(result.d_name,"..");
  99.             ++dirp->dd_index;
  100.         }
  101.         else if (dirp->dd_index == 0) {
  102.             strcpy(result.d_name,".");
  103.             ++dirp->dd_index;
  104.         }
  105.         else {
  106.             pb1.hFileInfo.ioVRefNum = dirp->dd_vRefNum;
  107.             pb1.hFileInfo.ioDirID = dirp->dd_drDirID;
  108.             pb1.hFileInfo.ioFDirIndex = dirp->dd_index++;
  109.             pb1.hFileInfo.ioNamePtr = (unsigned char *)result.d_name;
  110.             err = PBGetCatInfo(&pb1,0);
  111.             if (err) { set_errno(err); return 0L; };
  112.             PtoCstr((unsigned char *)result.d_name);
  113.         }
  114.     }
  115.  
  116.     result.d_namlen = strlen(result.d_name);
  117.     slashes_to_colons(result.d_name,result.d_namlen);
  118.     result.d_ino = 1;
  119.     return &result;
  120. }
  121.